<template>
  <section class="loginPageSty">
    <h1>欢迎访问陶礼俊的博客</h1>
    <el-form :model="form" label-width="auto" class="formSty" v-if="showLogin">
      <el-form-item label="name">
        <el-input v-model="form.name" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="password">
        <el-input v-model="form.password" placeholder="请输入密码" @keyup.enter="onSubmit"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="goRegister">去注册</el-button>
        <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>

    <el-form :model="register" label-width="auto" class="formSty" v-if="ShowRegister">
      <el-form-item label="昵称">
        <el-input v-model="register.name" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="QQ号码">
        <el-input v-model="register.qq" placeholder="仅为了获取QQ头像"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="register.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input
          v-model="register.passwordTwo"
          placeholder="请确认密码"
          @keyup.enter="onSubmitRegister"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="goLogin">去登录</el-button>
        <el-button type="primary" @click="onSubmitRegister">注册</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>
<script lang="ts" setup>
import { reactive, ref } from "vue";
import { loginApi, registerApi } from '../../api/index'
import { router } from "../../router";
import { ElMessage } from 'element-plus'
const showLogin = ref(true)
const ShowRegister = ref(false)
const form = reactive({
  name: '',
  password: ''
})
const register = reactive({
  name: '',
  password: '',
  passwordTwo: '',
  qq: ''
})
function goRegister() {
  showLogin.value = false;
  ShowRegister.value = true
}
function goLogin() {
  showLogin.value = true;
  ShowRegister.value = false
}

function onSubmit() {
  loginApi(form).then((res: any) => {
    if (res.status == 200) {
      ElMessage({
        message: '登录成功',
        type: 'success',
      })
      sessionStorage.setItem('token', res.data[0].token)
      sessionStorage.setItem('name', res.data[0].name)
      sessionStorage.setItem('userId', res.data[0].id)
      router.push('/index')
    } else {
      ElMessage.error(res.message)
    }

  })
}
function onSubmitRegister() {
  registerApi(register).then((res: any) => {
    if (res.status == 200) {
      ElMessage({
        message: '注册成功',
        type: 'success',
      })
      goLogin()
    } else {
      ElMessage.error(res.message)
    }
  })
}
</script>

<style scoped>
.loginPageSty {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  background: url("../../assets/login.png") 50% / cover no-repeat;
}
.formSty {
  width: 5rem;
}
h1 {
  font-size: 0.6rem;
  color: #fff;
  margin-bottom: 1rem;
}
</style>